using System;
using System.Diagnostics;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;
using CPlatePrintSys.Annotation.Events;
using Prism.Events;

namespace CPlatePrintSys.Annotation.Converters
{
    /// <summary>
    /// 相机同步扩展方法
    /// 提供便捷的相机操作接口
    /// </summary>
    public static class CameraSyncExtensions
    {
        /// <summary>
        /// 相机同步管理器实例
        /// </summary>
        private static CameraSyncManager _cameraSyncManager;

        /// <summary>
        /// 初始化相机同步管理器
        /// </summary>
        /// <param name="eventAggregator">事件聚合器</param>
        public static void InitializeCameraSync(IEventAggregator eventAggregator)
        {
            if (_cameraSyncManager != null)
            {
                _cameraSyncManager.Dispose();
            }

            _cameraSyncManager = new CameraSyncManager(eventAggregator);
            Debug.WriteLine("CameraSyncExtensions initialized");
        }

        /// <summary>
        /// 设置当前活跃的视口
        /// </summary>
        /// <param name="viewport">视口对象</param>
        /// <param name="controlId">控件ID</param>
        public static void SetAsActiveViewport(this HelixViewport3D viewport, string controlId)
        {
            if (_cameraSyncManager == null)
            {
                System.Diagnostics.Debug.WriteLine("CameraSyncManager未初始化，无法设置活跃视口");
                return;
            }

            if (string.IsNullOrEmpty(controlId))
            {
                System.Diagnostics.Debug.WriteLine("控件ID为空，无法设置活跃视口");
                return;
            }

            try
            {
                _cameraSyncManager.SetCurrentActiveViewport(controlId);
                //System.Diagnostics.Debug.WriteLine($"设置活跃视口成功: {controlId}");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置活跃视口异常: {ex.Message}");
                System.Diagnostics.Debug.WriteLine($"异常堆栈: {ex.StackTrace}");
            }
        }

        /// <summary>
        /// 注册视口到相机同步管理器
        /// </summary>
        /// <param name="viewport">视口对象</param>
        /// <param name="controlId">控件ID</param>
        public static void RegisterForCameraSync(this HelixViewport3D viewport, string controlId)
        {
            Debug.WriteLine($"=== 开始注册视口 ===");
            Debug.WriteLine($"控件ID: {controlId}");
            Debug.WriteLine($"视口对象: {viewport}");
            Debug.WriteLine($"CameraSyncManager状态: {_cameraSyncManager != null}");
            
            if (_cameraSyncManager == null)
            {
                Debug.WriteLine("CameraSyncManager未初始化，无法注册视口");
                return;
            }

            if (string.IsNullOrEmpty(controlId))
            {
                Debug.WriteLine("控件ID为空，无法注册视口");
                return;
            }

            if (viewport == null)
            {
                Debug.WriteLine("视口对象为空，无法注册视口");
                return;
            }

            try
            {
                _cameraSyncManager.RegisterViewport(controlId, viewport);
                Debug.WriteLine($"视口注册成功: {controlId}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"视口注册异常: {ex.Message}");
                Debug.WriteLine($"异常堆栈: {ex.StackTrace}");
            }
            
            Debug.WriteLine($"=== 视口注册完成 ===");
        }

        /// <summary>
        /// 注销视口
        /// </summary>
        /// <param name="viewport">视口对象</param>
        /// <param name="controlId">控件ID</param>
        public static void UnregisterFromCameraSync(this HelixViewport3D viewport, string controlId)
        {
            if (_cameraSyncManager == null)
                return;

            _cameraSyncManager.UnregisterViewport(controlId);
        }

        /// <summary>
        /// 同步相机位置
        /// </summary>
        /// <param name="viewport">源视口</param>
        /// <param name="controlId">控件ID</param>
        public static void SyncCameraPosition(this HelixViewport3D viewport, string controlId)
        {
            if (_cameraSyncManager == null || viewport?.Camera == null)
                return;

            var camera = viewport.Camera as ProjectionCamera;
            if (camera != null)
            {
                _cameraSyncManager.PublishCameraSync(controlId, CameraSyncOperation.Position, camera);
            }
        }

        /// <summary>
        /// 同步相机方向
        /// </summary>
        /// <param name="viewport">源视口</param>
        /// <param name="controlId">控件ID</param>
        public static void SyncCameraDirection(this HelixViewport3D viewport, string controlId)
        {
            if (_cameraSyncManager == null || viewport?.Camera == null)
                return;

            var camera = viewport.Camera as ProjectionCamera;
            if (camera != null)
            {
                _cameraSyncManager.PublishCameraSync(controlId, CameraSyncOperation.Direction, camera);
            }
        }

        /// <summary>
        /// 同步相机缩放
        /// </summary>
        /// <param name="viewport">源视口</param>
        /// <param name="controlId">控件ID</param>
        public static void SyncCameraZoom(this HelixViewport3D viewport, string controlId)
        {
            if (_cameraSyncManager == null || viewport?.Camera == null)
                return;

            var camera = viewport.Camera as ProjectionCamera;
            if (camera != null)
            {
                _cameraSyncManager.PublishCameraSync(controlId, CameraSyncOperation.Zoom, camera);
            }
        }

        /// <summary>
        /// 完整相机同步
        /// </summary>
        /// <param name="viewport">源视口</param>
        /// <param name="controlId">控件ID</param>
        public static void SyncCameraFull(this HelixViewport3D viewport, string controlId)
        {
            if (_cameraSyncManager == null || viewport?.Camera == null)
                return;

            var camera = viewport.Camera as ProjectionCamera;
            if (camera != null)
            {
                _cameraSyncManager.PublishCameraSync(controlId, CameraSyncOperation.Full, camera);
            }
        }

        /// <summary>
        /// 智能相机同步（根据变化类型自动选择同步操作）
        /// </summary>
        /// <param name="viewport">源视口</param>
        /// <param name="controlId">控件ID</param>
        /// <param name="changeType">变化类型</param>
        public static void SmartCameraSync(this HelixViewport3D viewport, string controlId, CameraChangeType changeType)
        {
            if (_cameraSyncManager == null || viewport?.Camera == null)
                return;

            var camera = viewport.Camera as ProjectionCamera;
            if (camera == null)
                return;

            CameraSyncOperation operation;
            switch (changeType)
            {
                case CameraChangeType.Position:
                    operation = CameraSyncOperation.Position;
                    break;
                case CameraChangeType.Direction:
                    operation = CameraSyncOperation.Direction;
                    break;
                case CameraChangeType.Zoom:
                    operation = CameraSyncOperation.Zoom;
                    break;
                case CameraChangeType.Multiple:
                    operation = CameraSyncOperation.Full;
                    break;
                default:
                    operation = CameraSyncOperation.Full;
                    break;
            }

            _cameraSyncManager.PublishCameraSync(controlId, operation, camera);
        }

        /// <summary>
        /// 相机变化类型
        /// </summary>
        public enum CameraChangeType
        {
            /// <summary>
            /// 位置变化
            /// </summary>
            Position,
            
            /// <summary>
            /// 方向变化
            /// </summary>
            Direction,
            
            /// <summary>
            /// 缩放变化
            /// </summary>
            Zoom,
            
            /// <summary>
            /// 多种变化
            /// </summary>
            Multiple
        }

        /// <summary>
        /// 清理资源
        /// </summary>
        public static void Cleanup()
        {
            if (_cameraSyncManager != null)
            {
                _cameraSyncManager.Dispose();
                _cameraSyncManager = null;
            }
        }
    }
}
